package com.spddr.pmos.rpt.impl;

import java.util.List;
import java.util.stream.Collectors;

import org.joda.time.Days;
import org.joda.time.Hours;
import org.joda.time.Period;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import com.spddr.pmos.domain.Metrics;
import com.spddr.pmos.rpt.RptReservoirSupplyLevel;
import com.spddr.pmos.rpt.pojo.CellValue;

import cn.seqdata.echarts.Legend;
import cn.seqdata.echarts.Option;
import cn.seqdata.echarts.axis.CategoryAxis;
import cn.seqdata.echarts.axis.ValueAxis;
import cn.seqdata.echarts.data.Data;
import cn.seqdata.echarts.series.Line;
import cn.seqdata.echarts.series.MarkLine;
import cn.seqdata.tags.Point;
import cn.seqdata.tags.common.IdTags;

@Service
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public class RptReservoirSupplyLevelImpl extends AbstractPivotChartImpl implements RptReservoirSupplyLevel {

	protected Period intervalOfStorage() {
		return Days.ONE.equals(interval) ? Hours.ONE.toPeriod() : Days.ONE.toPeriod();
	}

	@Override
	public List<CellValue> loadPivot() {
		metric = Metrics.ALTITUDE.name;
		Point<IdTags> point = new Point<>(metric, new IdTags(id));
		return sim(1).getValue().getValues().stream().map(v -> new CellValue(point, v, null, null))
				.collect(Collectors.toList());
	}

	@Override
	public Option chart() {
		Option option = new Option();

		CategoryAxis xAxis = new CategoryAxis().show(true);
		xAxis.setFunc(String.format("Admin.Array.dates(1, 'm-d', '%1$s', '%2$s')", getStartTime(), getEndTime()));
		option.xAxis(xAxis);

		option.yAxis(new ValueAxis().show(true));

		option.legend().data("控制计划", "实际水位").show(true);

		Line plan = new Line("控制计划");
		option.series().add(plan);
		// load(TreeLevel.Reservoir.id, id, Metrics.ALTITUDE.name).
		sim(1).getValue().getValues().forEach(x -> {
			plan.data().add(String.format("%.2f", x.getValue()));
		});
		// 实际水位
		Line actual = new Line("实际水位");

		MarkLine markLine = actual.markLine();
		markLine.itemStyle().normal().label().show(true);
		markLine.itemStyle().normal().color("#ff0000");
		markLine.data(new Data("死水位").setyAxis(100), new Data("防洪水位").setyAxis(700), new Data("正常蓄水水位").setyAxis(800));
		
		option.series().add(actual);
		// load(TreeLevel.Reservoir.id, id, Metrics.ALTITUDE.name).
		sim(1).getValue().getValues().forEach(x -> {
			actual.data().add(String.format("%.2f", x.getValue()));
		});

		return option;
	}

}
